/*
 * @(#)CVMOffsetsHeader.java	1.15 06/10/10
 *
 * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved.  
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER  
 *   
 * This program is free software; you can redistribute it and/or  
 * modify it under the terms of the GNU General Public License version  
 * 2 only, as published by the Free Software Foundation.   
 *   
 * This program is distributed in the hope that it will be useful, but  
 * WITHOUT ANY WARRANTY; without even the implied warranty of  
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  
 * General Public License version 2 for more details (a copy is  
 * included at /legal/license.txt).   
 *   
 * You should have received a copy of the GNU General Public License  
 * version 2 along with this work; if not, write to the Free Software  
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  
 * 02110-1301 USA   
 *   
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa  
 * Clara, CA 95054 or visit www.sun.com if you need additional  
 * information or have any questions. 
 *
 */

package runtime;
import consts.Const;
import jcc.Util;
import util.*;
import components.*;
import vm.*;
import java.io.PrintStream;
import java.util.Enumeration;
import java.util.Vector;
import java.util.Hashtable;

public class CVMOffsetsHeader extends HeaderDump {

    private String cdelimClassName = null;

    public CVMOffsetsHeader( ){
	super( '_' );
    }

    private void prolog(){
	o.println("/* DO NOT EDIT THIS FILE - it is machine generated */\n" +
	    "#include \"javavm/include/defs.h\"\n" +
	    "#include \"javavm/include/objects.h\"\n" +
	    "#include \"javavm/include/classes.h\"\n" );
	o.println("/* Header for class "+className+" */\n");
	String includeName = "_CVM_OFFSETS_"+cdelimClassName;
	o.println("#ifndef "+includeName);
	o.println("#define "+includeName+"\n");
    }

    private void epilog(){
	o.println("\n#endif");
    }

    synchronized public boolean
    dumpHeader( ClassInfo c, PrintStream outfile ){
	boolean didWork = false;
	o = outfile;
	className = c.className;
	cdelimClassName = strsub(className, CDelim);
	prolog();
	// Note: Should deal with > 255 fields.
	// Today is not that day. Just do the simple case.
	FieldInfo m[] = c.fields;
	if ( (m == null) || (m.length== 0)){
	    outfile.println("/* No fields for class "+cdelimClassName+" */\n");
	    return true;
	}
	int nfields = m.length;
	String prefix = "CVMoffsetOf"+cdelimClassName+"_";
	for ( int i = 0; i < nfields; i++ ){
	    FieldInfo f = m[i];
	    // static members use simple byte offset
	    // instance members include size of header info
	    if ( !f.isStaticMember() ){
		didWork = true;
		outfile.print("#define ");
		outfile.print(prefix + f.name.string + " \t");
		outfile.println("CVM_FIELD_OFFSET("+f.instanceOffset+")");
	    } 
	}
	if (!didWork) {
	    outfile.println("/* No instance fields for class "+cdelimClassName+" */\n");
	}
	epilog();
	return true;
    }

}
